﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECRMS.WinForm.WinService;

namespace ECRMS.WinForm.BaseClass.Common
{
    public class IndexOfResult
    {
        //全部数据
        public List<CtIndexResult> IndexResults;

        //总体指标
        public List<CtIndexResult> IndexTotalResults;
        //一级指标
        public List<CtIndexResult> IndexFirstResults;
        //二级指标
        public List<CtIndexResult> IndexSecondResults;

        //一级、二级数据的指标组
        public List<CtIndexGroup> IndexGroups;

        public FuncResult GetAllIndexResults(Guid corpId)
        {
            using (var ser = new ECRServiceClient())
            {
                CtServiceResultOfArrayOfCtIndexResultOUJwPUXg result = ser.CtGetIndexResultsByGroupId(corpId);
                if (result.Status == 1)
                {
                    this.IndexResults = result.Data;
                    SortResult();
                }
                return new FuncResult
                {
                    Status = result.Status,
                    Message = result.Message
                };
            }
        }

        //整理出来
        //这里是筛选出整套、一级、二级指标组成绩的数据
        private void SortResult()
        {
            this.IndexTotalResults = new List<CtIndexResult>();
            this.IndexFirstResults = new List<CtIndexResult>();
            this.IndexSecondResults = new List<CtIndexResult>();
            foreach (CtIndexResult item in IndexResults)
            {
                switch (item.Type)
                {
                    case IndexResultTypeEnum.AllIndex:
                        this.IndexTotalResults.Add(item);
                        break;
                    case IndexResultTypeEnum.FirstLevel:
                        this.IndexFirstResults.Add(item);
                        break;
                    case IndexResultTypeEnum.SecondLevel:
                        this.IndexSecondResults.Add(item);
                        break;
                    default:
                        break;
                }
            }
            SortIndexGoup();
        }

        private void SortIndexGoup()
        {
            IndexGroups = new List<CtIndexGroup>();
            //先遍历指标组
            foreach (CtIndexResult item in IndexTotalResults)
            {
                if (IndexGroups.FirstOrDefault(p=>p.IndexGroupRefId==item.IndexGroupRefId) == null)
                {
                    IndexGroups.Add(new CtIndexGroup { 
                        IndexGroupRefId=item.IndexGroupRefId,
                        IndexGroupName=item.IndexGroupName
                    });
                }
            }
        }
    }


}
